Operating Systems
Two main purposes:
- Turn hardware components into usable devices
- Make efficient use of resources (particularly when shared between processes)
Common general-purpose OSs:
- Windows
- Unix
- Linux
- MacOS
- iOS
- Android
Embedded operating systems exist in home appliances, TV boxes, game consoles, etc.
The four essential managers of every operating system:
- Processor Manager
- File Manager
- Device Manager
- Memory Manager
These all communicate with each other, and the User Interface (shell)
Each manager must perform certain tasks:
- Continuous monitoring of resources
- Enforcement of policies (who/when/how much resources for each process)
- Allocation of resources when appropriate
- Deallocation of resources when no longer needed
Managers must work in harmony with each other to complete tasks
Processes & Programs
A program is the code that performs some task (algorithm, etc.)
- Source code (Java, C++, etc.)
- Object code (compiled executable) stored on the disk
- Programs are static after compiled (they don't change)
A process is the activity that the CPU performs when it executes a program - Code is loaded from disk into memory
- Instruction pointer starts at first instruction in memory
- Processes are dynamic (execution branches depending on input)
OS Structure
- A central *kernel
- Exists permanently in memory
- Performs low-level, frequently needed activities (such as context switching)
- Operates in kernel mode (privileged mode)
- A shell
- Provides UI for the OS
- Allows the user to run and interact with programs (processes)
- Operated in user mode (no privileges, restricted access)
- A set of processes
- May be created by kernel to carry out its activities
- May be executed when user runs software
- Can be privileged or non-privileged depending on how they were started
System Boot (Initialisation)
When the device is turned on:
- Interrupt sent to CPU
- Instruction pointer set to the first address in ROM
The ROM contains a small bootstrap program:
- Performs basic system checks
- Sets up system bus and I/O channels
- Loads OS kernel from disk and passes control to it (via instruction pointer)
Kernel performs further setup and system checks
- Starts various processes to perform background tasks of the OS
- Starts the main shell process (from which further processes may be triggered)
Command Interpreter (Shell)
The shell is a collection of processes that are spawned by the kernel
- Allows the user to interact with the OS
- Processes run in user mode
- User can run more processes by typing a command or clicking an app icon
Before:
- Computers were big and expensive
- Users connected to a central mainframe via a text-based terminal
Now: - Linux, MacOS, Windows all provide a text-based view (terminal or DOS)
- But they also have graphical shells (desktop metaphor)
Protection Levels
The kernel has access to all parts of the CPU, every component, and every I/O device
- Must be protected from unauthorised access by users (hackers, malware, etc.)
- Kernel mode is enforced via a protection ring (Intel x86 processors have 4 rings)
- The CPU flags register stores the privilege level of each process
For Intel x86:
- 3 - user mode
- 2 - other device drivers
- 1 - trusted device drivers
- 0 - kernel mode
Certain registers, instructions, and memory addresses are protected
- They can only be used by processes with the correct privilege level
- Interrupt generated if privilege level doesn't match (general protection fault)
Processor Manager
The processor manager decides how to allocate the CPU to waiting processes
- Creates processes when a program is executed
- Initialises memory and stack for new processes
- Keeps track of the status of processes
- Assigns processes to the CPU when available (context switch)
- Changes process states as events occur
- Handles termination of processes on completion or abort
- Handles inter-process communication
- Managers process queues and prioritisation (scheduling)
Multiprogramming
Early OSs facilitated multiprogramming
- Load several processes into memory simultaneously (sharing the CPU)
- When running process can't continue (eg. waiting for I/O), switch to another
- So I/O and computation can overlap, so the CPU is always in use (executing something)
Problems aruse with compute-bound and I/O-bound processes
Multitasking (Time sharing)
This extends the concept of multiprogramming by providing fair access to the CPU
- OS switches rapidly between processes to give illusion of uninterrupted execution in parallel
- Each running process is given a fixed time slice (quantum) on the CPU
- The OS itself needs time on the CPU to perform its own scheduling tasks
Interrupt Handling
Multitasking depends on the ability to interrupt the CPU at regular intervals
- An interrupt request (IRQ) is a hardware signal
- Usually occurs because something happened outside normal program execution
- Can happen at any time, regardless of what the CPU is doing
- Tells CPU to stop current process execution and load an interrupt handler
CPU has an interrupt vector - Stores the memory address of handler for each type of interrupt
- Populated by OS when it first boots up
- The OS is responsible for handling and managing each interrupt
- Interrupt handlers are also known as interrupt service routines (ISR)
IRQ0 is the clock interrupt (quantum tick)
Context Switch
The clock interrupt is triggered at the end of each time slice (quantum)
- OS runs its scheduling algorithm to choose the next process
- This is called a context switch
During context switch, current state of CPU (registers) is saved in a data structure called the process control block (PCB) - OS stores a PCB in memory for each process
- When process is placed onto CPU, its registers are restored from its PCB
Context switches need time on CPU - Wastes a few CPU cycles to perform switch
- However allows for multitasking, without issues of multiprogramming
Process Control Block (PCB)
The kernel maintains a PCB for every process
- Stored in memory
- Can be represented as a file
Contains info such as - Unique process ID
- User ID of process owner
- Process state
- Memory address of process
- Accounting statistics (time used, etc.)
- Resources allocated to process (open files, network connections, devices, etc.)
- Register values from context switch (so the CPU can be restored)
Process States
A process goes through many state changes during its lifetime
OS must keep track and update PCB accordingly
- Running - Currently being executed by CPU
- Ready - Able to run, waiting for CPU to become available
- Blocked - Waiting for I/O to complete
Blocked processes are unavailable for despatch to the CPU
Ready processes selected for despatch according to a scheduling algorithm
Processor manager responsible for creating and terminating processes - Creation - Reserve memory for the process and its stack, set up PCB, initialise I/O channels, place process into ready state
- Termination - Close any open I/O channels, remove PCB, deallocate memory
![[Pasted image 20250516102502.png]]